/*
 * @Author: 张小平
 * @Date:   2025-05-01 
 * @Email:  qqshuqian@163.com 
 * @Copyright © 2024 aseyun. All rights reserved.
 */
import { suffix } from '@/framework-code/utils/constants'

const genId = () => {
  let id = (new Date()).getTime()
  let randomNumber = Math.floor(Math.random() * 100000)
  return `${id}${randomNumber}${suffix}`
}

const findById = (tree, id, deep = 0, pathIndex = '') => {
  let node = null
  let index = null
  let pathDeep = deep
  if (tree.id === id) {
    node = tree
  } else {
    pathDeep += 1
  }

  const treeChildren = tree['childList']

  if (treeChildren && treeChildren.length) {
    for (let i = 0; i < treeChildren.length; i++) {
      const child = treeChildren[i]
      pathIndex += `${i},`
      const recTree = findById(child, id, pathDeep, pathIndex)
      if (recTree.node) {
        node = recTree.node
        index = i
        pathDeep = recTree.pathDeep
        pathIndex = recTree.pathIndex
        break
      } else {
        pathIndex = pathIndex.slice(0, -2)
      }
    }
  }

  return {
    node,
    index,
    pathDeep,
    pathIndex
  }
}

const getParentNode = (tree, currentNode) => {
  const {
    pathIndex
  } = currentNode

  let paths = pathIndex.slice(0, -1).split(',')
  let str = 'tree'

  let pathsLen = paths.length - 1

  if (paths.length) {
    for (let i = 0; i < pathsLen; i++) {
      str += `['childList'][${paths[i]}]`
    }
  }

  return {
    parentNode: eval(str),
    paths
  }
}

const deleteNode = (tree, currentNode) => {
  const { parentNode, paths } = getParentNode(tree, currentNode)

  const pathLast = paths[paths.length - 1]

  const nodesNumber = parentNode.childList.length

  const children = currentNode.node.childList

  if (nodesNumber === 1) {
    parentNode.childList = children
  } else if (nodesNumber > 1) {
    switch (children.length) {
      case 0: {
        parentNode.childList[pathLast].id = genId()
        parentNode.childList[pathLast].childList = null
        parentNode.childList[pathLast].type = ''
        break
      }
      case 1: {
        parentNode.childList[pathLast].id = children[0].id
        parentNode.childList[pathLast].childList = children[0].childList
        parentNode.childList[pathLast].type = children[0].type
        break
      }
      default:
    }
    // parentNode.childList[pathLast] = children
  } else {
    console.log('todo')
  }

  return tree
}

const deleteBranch = (tree, currentNode) => {
  const { parentNode } = getParentNode(tree, currentNode)

  parentNode.childList.splice(currentNode.index, 1)

  return tree
}

const addNode = (tree, currentNode, newNode) => {
  const { parentNode, paths } = getParentNode(tree, currentNode)

  // console.log(parentNode, paths)

  // return false
  const pathLast = paths[paths.length - 1]

  const nodesNumber = parentNode.childList.length

  const children = currentNode.node.childList

  const id = genId()
  const type = newNode.type

  const newNodeArr = [{
    id,
    type,
    childList: currentNode.node.childList
  }]

  if (nodesNumber === 1) {
    parentNode.childList[0].childList = newNodeArr
  } else if (nodesNumber > 1) {
    switch (children.length) {
      case 1: {
        parentNode.childList[pathLast].childList = newNodeArr
        break
      }
      default:
    }
  }

  return tree
}

export default {
  findById,
  deleteNode,
  deleteBranch,
  addNode
}
